package thread;

import java.util.concurrent.TimeUnit;



/**
 * 开个线程，两秒后停止
 */
public class StopThread {
	private static volatile boolean stopRequested;
	public static void main(String[] args) throws InterruptedException {
		Thread backgroundThread = new Thread(new Runnable() {
			public void run() {
				int i = 0;
				while (!stopRequested) {
					i++;
				}
				System.out.println(i);
			}
		});
		backgroundThread.start();
		TimeUnit.SECONDS.sleep(2);
		stopRequested = true;
	}
}